#include "Validation_Sys.h"
#include "matrixkeys_control.h"

unsigned char try = 0;        // 判断是否输入
unsigned char temp[10] = {0}; // 用于存储用户输入
unsigned char password[10];   // eeprom中存储的真实密码
unsigned char mode = 0;       // 判断当前用户模式 
unsigned int addr = 0x0000;

static void login() { // 登录界面
    OLED_ShowString(0, 0, "enter your PIN:", 16);
}

static void setting() { // 修改密码界面
    OLED_ShowString(0, 0, "Enter New:", 16);
}
static void ClearTemp() {
    char i;
    for (i = 0; i < sizeof(temp); i++) { // 清空temp数组
        temp[i] = '\0';
    }
}

char Validation_Sys() { // 验证系统

    if (mode == 0)
        login();
    else if (mode == 1) {
        OLED_ShowString(0, 0, "welcome", 16);
        
        return 0x01;
    } else if (mode == 2) {
        setting();
    }
    return 0x00;
}
void Key_Is_Pressed(char row, char column) {
    static unsigned char i = 0;
    unsigned char index = row * COL_NUM + column;
    if (index <= 8) {
        if (try == 1) {
            OLED_Clear();
            try = 0;
        }
        temp[i++] = index + 1 + '0';
    }else if (index == 9) {
        if (try == 1) {
            OLED_Clear();
            try = 0;
        }
        temp[i++] = '0';
    }else if (index == 10) {
        temp[--i] = '\0';
        OLED_Clear();
    }
    OLED_ShowString(0, 2, temp, 16);

    if (index == 11) { // 确认键
        if (mode == 0) {
            EEPROM_Read(addr, password, 7);
            if (strcmp(password, temp) == 0) {
                mode = 1;
                OLED_ShowString(0, 2, "loging.", 16);
                os_wait2(K_TMO,40);
                OLED_ShowString(0, 2, "loging..", 16);
                os_wait2(K_TMO,40);
                OLED_ShowString(0, 2, "loging...", 16);
                os_wait2(K_TMO,40);
                OLED_ShowString(0, 2, "loging....", 16);
                os_wait2(K_TMO,40);
                OLED_Clear();
                // 亮灯
                ClearTemp();
                i = 0;
            } else {
                try = 1;
                OLED_ShowString(0, 4, "password error!", 16);
                Motor_Warn();//马达震动反馈
            }
        } else if (mode == 2) {
            EEPROM_Erase(addr);
            EEPROM_Write(addr, temp, strlen(temp) + 1);
            mode = 0;
            OLED_ShowString(0, 2, "setting.", 16);
            os_wait2(K_TMO,40);
            OLED_ShowString(0, 2, "setting..", 16);
            os_wait2(K_TMO,40);
            OLED_ShowString(0, 2, "setting...", 16);
            os_wait2(K_TMO,40);
            OLED_ShowString(0, 2, "setting....", 16);
            os_wait2(K_TMO,40);
            ClearTemp();
            i = 0;
            OLED_Clear();
        }
    }
    if (index == 12) { // 取消键
        OLED_Clear();
        ClearTemp();
        i = 0;
    }

    if (index == 13) { // 重置密码为123456
        OLED_Clear();
        EEPROM_Erase(addr);
        EEPROM_Write(addr, "123456", 7);
        OLED_ShowString(0, 2, "reseting.", 16);
        os_wait2(K_TMO,40);
        OLED_ShowString(0, 2, "reseting..", 16);
        os_wait2(K_TMO,40);
        OLED_ShowString(0, 2, "reseting...", 16);
        os_wait2(K_TMO,40);
        OLED_ShowString(0, 2, "reseting....", 16);
        os_wait2(K_TMO,40);
        mode = 0;
        ClearTemp();
        i = 0;
        OLED_Clear();
    }

    if (index == 14) { // 修改密码
        EEPROM_Read(addr, password, 7);
        if (strcmp(password, temp) == 0) {
            mode = 2;
            ClearTemp();
            i = 0;
            OLED_Clear();
        } else {
            try = 1;
            OLED_ShowString(0, 4, "password error!", 16);
            Motor_Warn();//马达震动反馈
        }
    }

    if (index == 15) {
        ClearTemp();
        i = 0;
        //灭灯
        mode = 0;
        OLED_Clear();
    }
}